package wnhz.com.business.groupshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.image.ImageOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.groupshop.bean.AllCommentBean;
import wnhz.com.business.groupshop.ui.AllCommentActivity;
import wnhz.com.business.groupshop.ui.DetailsActivity;
import wnhz.com.business.shareshop.activity.home.PersonalRemarkActivity;
import wnhz.com.business.shareshop.bean.ShopCommodityBean;
import wnhz.com.business.uitls.BroadCastReceiverUtil;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.CircularImage;
import wnhz.com.business.view.ExpandTextView;
import wnhz.com.business.view.NoScrollGridView;

public class AllCommentAdapter extends BaseRVAdapter {
    private BaseActivity context;


    private MyItemClickLinstener ClickLinstener;
    private List<ShopCommodityBean.InfoBean.CommontListBean> userTalkBeen = new ArrayList<>();
    /**
     * 构造器
     *
     * @param context 上下文
     * @param list    数据集合
     */
    public AllCommentAdapter(Context context, @NonNull List list) {
        super(context, list);
        this.context = (BaseActivity) context;
        userTalkBeen = list;
    }


    public void setOnClickListener(MyItemClickLinstener listener) {
        ClickLinstener = listener;
    }


    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_comment;
    }

    @Override
    public void onBind(final BaseViewHolder holder, final int position) {
        CircularImage iv_head = (CircularImage) holder.getImageView(R.id.iv_head);

        Glide.with(context).load(userTalkBeen.get(position).getHeadimg()).into(iv_head);
        Glide.with(context).load(userTalkBeen.get(position).getGoods_pic()).into(holder.getImageView(R.id.iv_img));
        holder.getImageView(R.id.iv_comment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.startActivity(new Intent(mContext, DetailsActivity.class)
                        .putExtra("tid",userTalkBeen.get(position).getComment_id()));
//                mContext.startActivity(new Intent(mContext, PersonalRemarkActivity.class));
            }
        });
//赞
        final ImageView iv_praise = holder.getImageView(R.id.iv_praise);
        TextView tv_comment = holder.getTextView(R.id.tv_comment);
        ImageView iv_comment = holder.getImageView(R.id.iv_comment);
        TextView tv_time = holder.getTextView(R.id.tv_time);
        TextView tv_name = holder.getTextView(R.id.tv_name);
        tv_name.setText(userTalkBeen.get(position).getNickname());
        ExpandTextView tv_userComment = holder.getView(R.id.tv_userComment);
        tv_userComment.setText(userTalkBeen.get(position).getContent());
        TextView tv_sku = holder.getTextView(R.id.tv_sku);
        tv_sku.setText(userTalkBeen.get(position).getSku_info());
        tv_time.setText(userTalkBeen.get(position).getAddtime());
        iv_comment.setTag(R.id.iv_comment, position);
        tv_comment.setTag(R.id.tv_comment, position);
        iv_comment.setOnClickListener(itemClickLinstener);
        tv_comment.setOnClickListener(itemClickLinstener);
        holder.getTextView(R.id.tv_comment).setText(userTalkBeen.get(position).getCounts());
        final TextView tv_praise = holder.getTextView(R.id.tv_praise);
        tv_praise.setText(userTalkBeen.get(position).getLikecount());
        holder.getTextView(R.id.pay_time).setText("购买时间:"+userTalkBeen.get(position).getPay_time());
        if(userTalkBeen.get(position).getIs_like().equals("0")){
            iv_praise.setBackgroundResource(R.drawable.ic_pinjia_undianzan);
        }else{
            iv_praise.setBackgroundResource(R.drawable.ic_pinjia_dianzaned);
        }
        iv_praise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpDianZanData(iv_praise,tv_praise, position);
            }
        });
        NoScrollGridView gridView = holder.getView(R.id.gridview);
        QuanZiImageAdapter ziImageAdapter = new QuanZiImageAdapter(context, userTalkBeen.get(position).getPic());
        gridView.setAdapter(ziImageAdapter);
    }




    public interface MyItemClickLinstener {
//        void OnDeleteClickListener(FragmentFifthAdapter adapter, int position, View view);

        void OnPingJiaClickListener(AllCommentAdapter adapter, int position, View view);
    }


    private View.OnClickListener itemClickLinstener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ClickLinstener != null) {
                int position;
                switch (v.getId()) {
                    case R.id.iv_comment:
                        position = (int) v.getTag(R.id.iv_comment);
                        ClickLinstener.OnPingJiaClickListener(AllCommentAdapter.this, position, v);
                        break;
//                    case R.id.iv_praise:
//                        position = (int) v.getTag(R.id.tv_comment);
//                        ClickLinstener.OnPingJiaClickListener(AllCommentAdapter.this, position, v);
//                        break;
                }
            }
        }
    };




    private void UpDianZanData(final ImageView img,final TextView tv, final int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
        map.put("commentId", userTalkBeen.get(position).getComment_id());
        XUtil.Post(Url.PINGJIA_DIANZAN, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    String info = jsonObject.optString("info");
                    if ("1".equals(re)) {
                        if(userTalkBeen.get(position).getIs_like().equals("0")){
                           img.setBackgroundResource(R.drawable.ic_pinjia_dianzaned);
                            tv.setText((Integer.parseInt(userTalkBeen.get(position).getLikecount())+1)+"");
                            userTalkBeen.get(position).setIs_like("1");
                            userTalkBeen.get(position).setLikecount((Integer.parseInt(userTalkBeen.get(position).getLikecount())+1)+"");
                        }else{
                            img.setBackgroundResource(R.drawable.ic_pinjia_undianzan);
                            tv.setText((Integer.parseInt(userTalkBeen.get(position).getLikecount())-1)+"");
                            userTalkBeen.get(position).setIs_like("0");
                            userTalkBeen.get(position).setLikecount((Integer.parseInt(userTalkBeen.get(position).getLikecount())-1)+"");
                        }
                        context.MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }
        });
    }

    private ImageView imageView;

    ImageOptions shopoptions = new ImageOptions.Builder()
            .setLoadingDrawableId(R.drawable.banner_default)
            //设置加载失败后的图片
            .setFailureDrawableId(R.drawable.banner_default)
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(false)
            //设置支持gif
            .setIgnoreGif(false)
            .build();
}
